class CustomersController < ApplicationController
  before_filter :user_authorize,:except=>[:getPassengerComboBox]
  layout "index",:except => [:customerInfoByID,:customerByPhoneNo,:addCustomer,:editCustomer,:getCustomerByID,:getPassengersByCustomerID,
    :checkCustomerNo,:getOrdersByCustomer,:searchByUserTypeView,:getPassengerComboBox,:getAddressComboBox,:getCustomersLikeName,
    :saveRemark,:customerHistoryList,:getCustomerHistory,:searchByUserBirthday,:getCustomersPoint,:reCountCustomersPoint,
    :getCustomerPointInfo,:saveCustomerPointInfo,:saveCustomerPointMod,:getPointHis]
  
  # Get All Customer Info By ID
  
  def customerList
    #权限
    if CommonHelper.rightAuthorization(session[:user_rights],params[:controller],params[:action]) == "false"
      redirect_to(:controller => "index", :action => "main") and return
    end 
  end
  
  def customerInfoByID
    begin
      id = params[:id]
      phone = params[:phone_number]
      no = params[:customer_no]
      
      @phone_number = ""
      if phone
        @phone_number = phone
      elsif session[:phone_number]
        @phone_number = session[:phone_number]
      end
      
      if id && id.to_i>0
        @customer = Customer.find_by_id(id,:include=>[:customer_type])
      elsif no && no!=""
        @customer = Customer.find(:first,:conditions=>["customer_no=?",no],:include=>[:customer_type])
      elsif @phone_number && @phone_number!=""
        @customer = Customer.find(:first,:conditions=>["phone=?",@phone_number],:include=>[:customer_type])
      end
      @customer = Customer.find_by_id(1,:include=>[:customer_type]) if @customer==nil
      
      #这儿是判断来电的,如果是真正的来电就添加到数据库,非来电,只是复现就不处理.
      if params[:type]=="new" && phone
        callerslog = CallersLog.new
        callerslog.customer_id = @customer.id
        callerslog.phone = phone
        callerslog.user_id = session[:user_id]
        callerslog.save
      end
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  # Get All Customer By PhoneNo
  def customerByPhoneNo
    begin
      
      phone = params[:phone_number]
      #customer = Customer.find(:first,:conditions=>["phone=?",phone]) if phone
      #redirect_to :controller => 'index',:action => 'phoneshow',:customerid=>customer.id,:phone_number=>phone and return
      redirect_to :controller => 'index',:action => 'phoneshow',:phone_number => phone and return
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  # Get All Customer By PhoneNo
  def customersByPhoneNo
    begin
      
      phone = Phone.find_by_phone_number(params[:phone_number])
      @phone_number=params[:phone_number]
      if phone.customers.size==1
        redirect_to :controller => 'index',:action => 'phoneshow',:customerid=>phone.customers[0].id,:phone_number=>@phone_number and return
      end
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def getCustomersByPhoneNo
    begin
      
      phone = Phone.find_by_phone_number(params[:phone_number])
      @customers = phone.customers
      jsonstr="{totalProperty:"+@customers.length.to_s+",root:"+@customers.to_json()+"}"
      
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #Add Customer
  def addCustomer
    begin
      #增加一个判断是不是已经有现有身份证号存在的情况。
      card_type_id = params[:card_type_id]
      customerNo = params[:customer_no]
      phone = params[:phone]
      card = params[:card]
      #这儿只判断电话的重复性．
      customer = Customer.find(:first,:conditions=>["phone = ?",phone])
      #customer = Customer.find(:first,:conditions=>["phone = ?","%#{phone}%"])
      if customer == nil
        #这儿判断证件等是不是有可能重复
        #如果会员类型和卡号为空，直接就认为是来电新加客户不用再判断证件重复．
        if card != ""
          if customerNo.length>0
            customer = Customer.find(:first,:conditions=>["customer_no like ? OR (card_type_id = ? AND card like ?)","%#{customerNo}%",card_type_id,"%#{card}%"])
          else
            customer = Customer.find(:first,:conditions=>["card_type_id = ? AND card like ?",card_type_id,"%#{card}%"])
          end
        end
        if customer == nil
          customer = Customer.new
          customer.name=params[:name]
          #是否为中文的判断
          rChineseChar = Regexp.new('[\u4e00-\u9fa5]')
          py = ""
          if (customer.name =~ rChineseChar) == nil
            nameLen = customer.name.chars.length
            for i in 0..nameLen-1
              singleCNChar = customer.name.chars[i,1]
              if EncodingChar.find(:first,:conditions=>["cn_char = ?",singleCNChar])
                py << EncodingChar.find(:first,:conditions=>["cn_char = ?",singleCNChar]).py[0,1]
              else
                CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],"#{singleCNChar},这个字没有发现.","error")
              end
            end
          end
          customer.py=py
          customer.sex=params[:sex]
          customer.phone=params[:phone]
          customer.birthday=params[:birthday]
          customer.customer_type_id=params[:customer_type_id]
          if card_type_id !=""
            customer.card_type_id = card_type_id
            #身份证为1
            if card_type_id == "1"
              customer.birthday = CommonHelper.formatBirthdayToDate(card)
            end
          end
          customer.card = card
          customer.customer_no = params[:customer_no]
          customer.address=params[:address]
          customer.linkman=params[:linkman]
          customer.linkman_phone=params[:linkman_phone]
          customer.customer_level=params[:customer_level]
          customer.user_id=session[:user_id]
          customer.save
          jsonstr="添加客户成功 -id="+customer.id.to_s+" -name="+customer.name.to_s
        else
          jsonstr="已经有相同证件类型和证件号或相同的会员卡号的用户存在！"
        end
      else
        jsonstr="已经有相同电话号码的用户存在！"
      end
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def getCustomers
    begin
      
      pageBegin =  params[:start]
      pageEnd =  params[:limit]
      strdelData = params[:delData]
      if strdelData!=nil
        delCustomer(strdelData)
      end
      #for search
      #customerType=params[:customer_type]
      searchKey =params[:searchkey]
      birthdayKey=params[:birthdayKey]
      #logger.error "customerType:"+customerType.to_s
      if birthdayKey!=nil
        customers = Customer.find(:all,:conditions => ["is_locked <> 1 AND id <> 1 AND month(birthday) = ?",birthdayKey.split("-")[1]],:limit =>pageBegin+","+pageEnd)
        pageCustomers = customers
      elsif searchKey == nil || searchKey == ""
        pageCustomers = Customer.find(:all,:conditions=>["is_locked <> 1 AND id <> 1"])
        customers = Customer.find(:all,:conditions=>["is_locked <> 1 AND id <> 1"],:limit =>pageBegin+","+pageEnd)
      else
        searchKey = "%"+searchKey+"%"
        pageCustomers = Customer.find(:all,:conditions => ["is_locked <> 1 AND id <> 1 and name like ? OR linkman like ?",searchKey,searchKey])
        customers = Customer.find(:all,:conditions => ["is_locked <> 1 AND id <> 1 and name like ? OR linkman like ?",searchKey,searchKey],:limit =>pageBegin+","+pageEnd)
      end
      jsonstr="{totalProperty:"+pageCustomers.length.to_s+",root:"+customers.to_json(:include=>[:customer_type,:card_type])+"}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #客户积分查询
  def getCustomersPoint
    begin
      
      customerNo = params[:customerNo]
      phone = params[:phone]
      name = params[:name]
      from = params[:from]
      to = params[:to]
      from_p = params[:from_p]
      to_p = params[:to_p]
      
      #      logger.error customerNo
      #      logger.error phone
      #      logger.error name
      #      logger.error from
      #      logger.error to
      #      logger.error from_p
      #      logger.error to_p
      
      pageBegin =  params[:start]
      pageEnd =  params[:limit]
      
      sql = ""
      if customerNo && customerNo!=""
        sql << " and customer_no="+customerNo
      end
      if phone && phone!=""
        sql << " and phone="+phone
      end
      if name && name!=""
        sql << " and (name like '%"+name+"%' or linkman like '%"+name+"%')"
      end
      if from && from!=""
        sql << " and point>="+from
      end
      if to && to!=""
        sql << " and point<="+to
      end
      if from_p && from_p!=""
        sql << " and expenditure>="+from_p
      end
      if to_p && to_p!=""
        sql << " and expenditure<="+to_p
      end
      
      num = 0
      rootStr = "[]"
      sql = " is_locked <> 1 and customer_type_id<>1 and card_type_id is not null" + sql
      logger.error sql
      pageCustomers = Customer.find(:all,:conditions => sql)
      num = pageCustomers.length if pageCustomers
      customers = Customer.find(:all,:conditions => sql,:limit =>pageBegin+","+pageEnd)
      rootStr = customers.to_json(:include=>[:customer_type,:card_type])
      
      jsonstr="{totalProperty:"+num.to_s+",root:"+rootStr+"}"
      
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end 
  end
  
  #重新计算客户积分等
  def reCountCustomersPoint
    begin
      
      result = "计算成功"
      sql = " is_locked <> 1 AND customer_type_id <> 1 "
      customers = Customer.find(:all,:conditions=>sql,:order=>"id")
      for customer in customers
        logger.error customer.name
        
        tail_sql = " customer_id="+customer.id.to_s
        tail_sql << " and status='"+Constants::ORDER_STATUS_YFK+"'"
        tail_sql << " and pay_type<>'"+Constants::PAY_TYPE_JF+"' and pay_type<>'"+Constants::PAY_TYPE_QT+"'"
        tail_sql << " and is_locked <> 1"
        
        sql = "select sum(price_sum) as sum from orders where "+tail_sql
        logger.error sql
        expenditure_sum = Order.find_by_sql(sql)[0].sum.to_i
        logger.error "累计消费="+expenditure_sum.to_s
        
        sql = "select sum(point) as point_sum,sum(price) as price_sum,sum(airdrome_tax) as tax_sum,sum(fuel_tax) as fuel_sum from tickets where status='"+Constants::TICKET_STATUS_YCP+"' and order_id in(select id from orders where "+tail_sql+")"
        logger.error sql
        
        ticket = Ticket.find_by_sql(sql)[0]
        point_sum = ticket.point_sum.to_i
        price_sum = ticket.price_sum.to_i
        tax_sum = ticket.tax_sum.to_i
        fuel_sum = ticket.fuel_sum.to_i
        
        logger.error "累计积分="+point_sum.to_s
        logger.error "累计票价="+price_sum.to_s
        logger.error "累计税费="+tax_sum.to_s
        logger.error "累计附加="+fuel_sum.to_s
        
        #统计出订单中所有积分和消费数据 减去积分维护记录中的数据 得到真实的积分和消费数据 再保存到客户表中
        sql = "select sum(point) as point_sum,sum(price) as price_sum,sum(expenditure) as expenditure_sum,sum(tax) as tax_sum,sum(fuel) as fuel_sum  from point_his where customer_id="+customer.id.to_s
        sql << " and is_locked<>1 and point<>0"
        #加
        pointHis = PointHis.find_by_sql(sql+" and options='加'")[0]
        point_sum_add = pointHis.point_sum.to_i
        price_sum_add = pointHis.price_sum.to_i
        expenditure_sum_add = pointHis.expenditure_sum.to_i
        tax_sum_add = pointHis.tax_sum.to_i
        fuel_sum_add = pointHis.fuel_sum.to_i
        #减
        pointHis = PointHis.find_by_sql(sql+" and options='减'")[0]
        point_sum_dec = pointHis.point_sum.to_i
        price_sum_dec = pointHis.price_sum.to_i
        expenditure_sum_dec = pointHis.expenditure_sum.to_i
        tax_sum_dec = pointHis.tax_sum.to_i
        fuel_sum_dec = pointHis.fuel_sum.to_i
        
        point = point_sum + point_sum_add - point_sum_dec
        price = price_sum + price_sum_add - price_sum_dec
        expenditure = expenditure_sum + expenditure_sum_add - expenditure_sum_dec
        tax = tax_sum + tax_sum_add - tax_sum_dec
        fuel = fuel_sum + fuel_sum_add - fuel_sum_dec
        
        logger.error "--当前积分="+point.to_s
        logger.error "--当前票价="+price.to_s
        logger.error "--当前消费="+expenditure.to_s
        logger.error "--当前税费="+tax.to_s
        logger.error "--当前附加="+fuel.to_s
        
        #将历史记录和当前记录加起来组成新的当前记录
        customer.point = point + customer.point_old
        customer.point_his = point_sum + customer.point_his_old
        customer.price = price + customer.price_old
        customer.expenditure = expenditure + customer.expenditure_old
        customer.expenditure_his = expenditure_sum + customer.expenditure_his_old
        customer.tax = tax + customer.tax_old
        customer.fuel = fuel + customer.fuel_old
        if !customer.save
          result = "重新计算客户"+customer.name+"的积分失败"
          logger.error result
        end
      end
      render :text=>result, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end 
  end
  
  def getCustomerPointInfo
    begin
      id = params[:id]
      logger.error id
      customer = Customer.find_by_id(id)
      if customer
        rootStr = customer.to_json()
      end
      logger.error rootStr
      jsonstr="{root:["+rootStr+"]}"
      
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #得到积分维护历史记录
  def getPointHis
    begin
      strdelData = params[:delData]
      if strdelData
        ids = ""
        results = ActiveSupport::JSON.decode(strdelData)
        results.each do |result|
          ids << "," if ids!=""
          ids << result["id"]
        end
        PointHis.delete_all("id in ("+ids+")")
        #PointHis.update_all("is_locked = 1","id in ("+ids+")")
      end
      
      rootStr = "[]"
      id = params[:id] #客户id
      ps = PointHis.find(:all,:conditions=>["customer_id=? and is_locked<>1",id],:order=>"created_at desc")
      if ps && ps.length>0
        rootStr = ps.to_json(:include=>[:customer,:user])
      end
      jsonstr="{root:"+rootStr+"}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #积分维护
  def saveCustomerPointMod
    begin
      id = params[:id]
      options = params[:options]
      point = params[:point]
      price = params[:price]
      expenditure = params[:expenditure]
      tax = params[:tax]
      fuel = params[:fuel]
      remark = params[:remark]
      
      pointHis = PointHis.new
      pointHis.customer_id = id.to_i
      pointHis.user_id = session[:user_id]
      pointHis.options = options
      pointHis.point = point.to_i
      pointHis.price = price.to_i
      pointHis.expenditure = expenditure.to_i
      pointHis.tax = tax.to_i
      pointHis.fuel = fuel.to_i
      pointHis.remark = remark
      if pointHis.save
        result = "{success:true,info:'保存成功'}"
      else
        result = "{success:false,info:'保存维护记录失败'}"
      end
      render :text=>result, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #初始化历史积分和消费 -- 小绿人中的历史记录
  def saveCustomerPointInfo
    begin
      id = params[:id]
      point_old = params[:point_old]
      point_his_old = params[:point_his_old]
      price_old = params[:price_old]
      expenditure_old = params[:expenditure_old]
      expenditure_his_old = params[:expenditure_his_old]
      tax_old = params[:tax_old]
      fuel_old = params[:fuel_old]
      
      result = "{success:false,info:'保存失败'}"
      customer = Customer.find_by_id(id)
      if customer
        customer.point_old = point_old
        customer.point_his_old = point_his_old
        customer.price_old = price_old
        customer.expenditure_old = expenditure_old
        customer.expenditure_his_old = expenditure_his_old
        customer.tax_old = tax_old
        customer.fuel_old = fuel_old
        if customer.save
          result = "{success:true,info:'保存成功'}"
        end
      end
      render :text=>result, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #Delete Customer
  def delCustomer(strdelData)
    ids=""
    begin
      
      results = ActiveSupport::JSON.decode(strdelData)
      results.each do |result|
        ids << result["id"]+","
      end
      ids = ids[0,ids.length-1]
      Customer.update_all("is_locked = 1","id in ("+ids+")")
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  # Get Customer By ID
  def getCustomerByID
    begin
      customer = Customer.find(params[:id])
      jsonstr="{root:["+customer.to_json(:include=>[:customer_type,:card_type])+"]}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def editCustomer
    begin
      card_type_id = params[:card_type_id]
      if card_type_id == ""
        original_customer = Customer.find(params[:id])
        card_type_id = original_customer.card_type_id
      end
      card = params[:card]
      customer = Customer.find(:first,:conditions=>["card_type_id = ? AND card like ? AND id <> ?",card_type_id,"%#{card}%",params[:id]])
      if customer == nil
        if original_customer == nil
          customer = Customer.find(params[:id])
        else
          customer = original_customer
        end
        #Clone for CustomerBackup
        customerBK = CustomerBackup.new
        customer.attributes.each do |attr, value|
          if attr !="created_at" && attr !="updated_at"
            eval("customerBK.#{attr}= customer.#{attr}")
          end
        end
        customerBK.id =nil
        customerBK.user_id =session[:user_id] #保存修改者的信息
        customerBK.customer_id=customer.id
        customerBK.save

        #如果用户改了名字，或由于某些原因，用户的拼音长度少于2的，从新生成拼音。
        if params[:name] != customer.name || customer.py.length < 2
          rChineseChar = Regexp.new('[\u4e00-\u9fa5]')
          customer.name=params[:name]
          if (customer.name =~ rChineseChar) == nil
            nameLen = customer.name.chars.length
            py = ""
            for i in 0..nameLen-1
              singleCNChar = customer.name.chars[i,1]
              py << EncodingChar.find(:first,:conditions=>["cn_char = ?",singleCNChar]).py[0,1]
            end
            customer.py=py
          end
        end
        customer.sex=params[:sex]
        customer.birthday=params[:birthday]
        customer.phone=params[:phone]
        if params[:customer_type_id]!=""
          customer.customer_type_id=params[:customer_type_id]
        end
        if card_type_id!=""
          customer.card_type_id=card_type_id
          if card_type_id == "1"
            customer.birthday = CommonHelper.formatBirthdayToDate(card)
          end
        end
        customer.card = card
        customer.customer_no = params[:customer_no]
        customer.address=params[:address]
        customer.linkman=params[:linkman]
        customer.linkman_phone=params[:linkman_phone]
        customer.customer_level=params[:customer_level]
        customer.user_id=session[:user_id]
        customer.save
        info = "编辑用户信息成功"
      else  
        info = "已经有相同证件类型和证件号的用户存在！"
      end
      jsonstr="{success:true,info:'#{info}'}"
      
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #根据客户ID取得此用户下操作过的所有常旅客
  def getPassengersByCustomerID
    
  end
  
  def saveRemark
    begin
      remark = params[:remark]
      customer_id = params[:customer_id]
      if customer_id !=nil
        customer=Customer.find(customer_id)
        customer.remark=remark
        customer.save
      end
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def checkCustomerNo
    begin
      customerNo=params[:customer_no]
      customer = Customer.find_by_customer_no(customerNo)
      if customer && customer.phone
        result = customer.phone
      else
        result = "失败"
      end
      render :text=>result, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def getOrdersByCustomer
    begin
      orders=Order.find(:all,:conditions=>["is_locked <> 1"])
      rootStr = "[]"
      if orders && orders.length>0
        rootStr = orders.to_json(:include=>[:customer,:creator,:ticket_outer,:printer,:cash_checker,:sender,:jj_man,:jj_affirmer])
      end
      jsonstr="{root:"+rootStr+"}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def getAllCustomerComboBox
    begin
      customers = Customer.find(:all,:conditions=>["is_locked <> 1 and customer_type_id<>1"])
      rootStr = customers.to_json()
      jsonstr="{root:"+rootStr+"}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def getPassengerComboBox
    begin
      customers = Customer.find(:all,:conditions=>["is_locked <> 1 AND (name like ? OR py like ?)","%#{params[:query]}%","%#{params[:query]}%"])
      rootStr = customers.to_json()
      jsonstr="{root:"+rootStr+"}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def getAddressComboBox
    begin
      rootStr = "[]"
      id = params[:id]
      orderid = params[:orderid]
      if id==nil && orderid
        order = Order.find_by_id(orderid)
        id = order.customer_id.to_s
      end
      if id
        if params[:query]==nil || params[:query]==""
          addresses = CustomerAddress.find(:all,:conditions=>["is_locked <> 1 AND customer_id=?",id])
        else
          query = params[:query]
          #如果有单引号 则只取单引号之前的字符
          query = query[0,query.index("\'")] if query.index("\'")
          query = query[0,query.index("\"")] if query.index("\"")
          #query = query.gsub(/'/,"\\\'")
          sql = "is_locked <> 1 AND customer_id="+id+" AND address like '%"+query+"%'"
          addresses = CustomerAddress.find(:all,:conditions=>sql)
          #addresses = CustomerAddress.find(:all,:conditions=>["is_locked <> 1 AND customer_id=? AND address like '%?%'",id,query])
        end
        rootStr = addresses.to_json()
      end
      jsonstr="{root:"+rootStr+"}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def getCustomersLikeName
    begin
      customers = Customer.find(:all,:conditions => ["phone<>'' and (name like ? OR py like ?) AND is_locked <> 1","%#{params[:query]}%","%#{params[:query]}%"])
      jsonstr="{totalProperty:"+customers.length.to_s+",root:"+customers.to_json()+"}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  def customerHistoryList
    @customer_id = params[:customer_id]
  end
  
  def getCustomerHistory
    begin
      
      logger.error params[:customer_id]
      customerHistory = CustomerBackup.find(:all,:conditions => ["customer_id = ?",params[:customer_id]])
      logger.error customerHistory.size
      logger.error customerHistory[0].name
      jsonstr="{totalProperty:"+customerHistory.length.to_s+",root:"+customerHistory.to_json(:include=>[:customer_type,:card_type,:user])+"}"
      
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  def searchByUserBirthday
    
  end
end
